package artid.com.arttogo.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.adapter.AuctionSpecial_Adapter;
import artid.com.arttogo.bean.SpecialListParser;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.view.MyListView;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableScrollView;

/**
 * 
 *    
 * 项目名称：ArtworkArt   
 * 类名称：AuctionSpecial_Activity   
 * 类描述：拍卖行-专场   
 * 创建人：耿卫斌   
 * 创建时间：2017-3-1 下午2:19:14   
 *
 */
public class AuctionSpecial_Activity extends BaseActivity implements OnClickListener {
	private ImageView searchBtn;
	private PullToRefreshLayout mPullToRefreshLayout;
	private PullableScrollView mPullableScrollView;
	private TextView specialNum;
	private MyListView mListView;
	private LinearLayout noDataLL;
	private AuctionSpecial_Adapter mAdapter;
	private boolean isRefreshed = false;// 是否刷新
	private int page = 1;
	private List<SpecialListParser> list=new ArrayList<SpecialListParser>();
	private List<SpecialListParser> more_list=new ArrayList<SpecialListParser>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_auctionspecial);
		initView();
	}

	private void initView(){
		mContext=AuctionSpecial_Activity.this;
		IntentFilter filter = new IntentFilter("com.AuctionSpecial_Activity.receiver");
		registerReceiver(broadcastReceiver, filter);
		mBack = (ImageView) findViewById(R.id.back_btn);
		mBack.setOnClickListener(this);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		mTittle.setText("专场");
		searchBtn=(ImageView) findViewById(R.id.AuctionSpecial_title_searchBtn);
		searchBtn.setOnClickListener(this);
		mPullToRefreshLayout = (PullToRefreshLayout)findViewById(R.id.refresh_layout);
		mPullableScrollView = (PullableScrollView)findViewById(R.id.AuctionSpecial_PullableScrollView);
		specialNum=(TextView) findViewById(R.id.AuctionSpecial_specialNum);
		mListView=(MyListView) findViewById(R.id.AuctionSpecial_ListView);
		mListView.setOnItemClickListener(onItemClick);
		noDataLL = (LinearLayout) findViewById(R.id.ll_hint);
		mPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// 下拉刷新
				page = 1;
				isRefreshed = true;
				getSpecialListData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// 上拉加载
				page++;
				getSpecialListData();	
			}
		});
		getSpecialListData();
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_btn:
			finish();
			break;
		case R.id.AuctionSpecial_title_searchBtn:
			//搜索
			intent=new Intent(mContext,LotSearchActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
	}
	
	/**
	 * 专场列表Item点击事件
	 */
	OnItemClickListener onItemClick=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
								long arg3) {
			// TODO Auto-generated method stub
			Intent intent=new Intent(mContext,AuctionSpecialInfo_Activity.class);
			intent.putExtra("SpecialID", more_list.get(position).getId());
			startActivity(intent);
		}

	};
	
	/**
	 * 获取专场列表数据
	 * 拍卖会id	id	
		页码	page	
		条数	pagesize	
		类型	type	默认是1，2表示往期拍卖会 /3 表示首页进入的热门专场
	 */
	private void getSpecialListData(){
		VolleyBasic.getInstance(this).post(Cache.specialList,
				VolleyMap.getInstance().specialList(page, 10, "", "3"),
				SpecialListParser.class, new VolleyCallback() {
			@SuppressWarnings({ "unchecked", "static-access" })
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				mPullToRefreshLayout.loadmoreFinish(mPullToRefreshLayout.SUCCEED);
				try {
					if(page==1){
						//当请求第二页的时候num不会返回值
						JSONObject obj = new JSONObject(vd.getJson());
						String num=obj.optString("num");
						specialNum.setText("所有专场（"+num+"）");
					}
					list=(List<SpecialListParser>) vd.getList();
					if (isRefreshed == true) {
						more_list.clear();
						isRefreshed = false;
					}
					if (!list.isEmpty()) {
						more_list.addAll(list);
					}
					//这里是显示有数据和无数据的布局
					if(more_list!=null && more_list.size()!=0){
						noDataLL.setVisibility(View.GONE);
					}else{
						noDataLL.setVisibility(View.VISIBLE);
						return;
					}
					if (mAdapter == null) {
						mAdapter=new AuctionSpecial_Adapter(mContext, more_list);
						mListView.setAdapter(mAdapter);
					}
					mAdapter.notifyDataSetChanged();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void onError(VolleyError volleyError) {
				// TODO Auto-generated method stub
				mPullToRefreshLayout.loadmoreFinish(mPullToRefreshLayout.FAIL);
				if(more_list!=null && more_list.size()!=0){
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}
			}
		});
	}
	
	/**
	 * 广播接收器
	 */
	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if (intent.getBooleanExtra("addRemind", false)) {
				//判断是否为添加提醒的广播通知
				String specialId=intent.getStringExtra("specialId");
				if(more_list!=null && more_list.size()!=0){
					//修改状态
					for (int i = 0; i < more_list.size(); i++) {
						if(more_list.get(i).getId().equals(specialId)){
							more_list.get(i).setIs_remind(2);
							mAdapter.notifyData(more_list);
						}
					}
				}
			};
		}
	};
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// 关闭广播
		unregisterReceiver(broadcastReceiver);
	}
}
